#loading packages
library('FactoMineR')
library('factoextra')
library("gplots")
library("ggrepel")
library("dplyr")
library("tidyverse")
library("ggplot2")
library("ggthemes")
library("forcats")
library("reshape2")

#Chapter 3

load(file=file="Data/Existent Data on Religion/Treated Data/Final Data/minorities.rdata") #Dataset used for the MCA - Minorities MCA

load(file= "Data/Existent Data on Religion/Treated Data/Final Data/Min.rdata") #Dataset used for the Score analysis - Minorityies MCA

#MCA for Minority Groups - Min_active dataset
load(file=file="Data/Existent Data on Religion/Treated Data/Final Data/minorities.rdata")

#MCA
mca <- MCA(Min_active, graph = FALSE)
print(mca)

#Religious Group Categorisation
##Extracting Individuals Data
df <-mca$ind$coord
df <- data.frame(df[1:896, 1:2])

##Selecting and summarising per groups

groups <- data.frame(df[1:64, 1:2])
groups <- summarise_each(groups, funs(mean))
rownames(groups) <- c("Buddhists")

cath <- data.frame(df[65:104, 1:2])
cath <- summarise_each(cath, funs(mean))
rownames(cath) <- c("Catholics")
groups <- dplyr::union(groups, cath)

cris <- data.frame(df[105:176, 1:2])
cris <- summarise_each(cris, funs(mean))
rownames(cris) <- c("Christians")
groups <- dplyr::union(groups, cris)

hin <- data.frame(df[177:192, 1:2])
hin <- summarise_each(hin, funs(mean))
rownames(hin) <- c("Hindus")
groups <- dplyr::union(groups, hin)

jws <- data.frame(df[193:216, 1:2])
jws <- summarise_each(jws, funs(mean))
rownames(jws) <- c("Jehova's Witness")
groups <- dplyr::union(groups, jws)

jews <- data.frame(df[217:432, 1:2])
jews <- summarise_each(jews, funs(mean))
rownames(jews) <- c("Jews")
groups <- dplyr::union(groups, jews)

mor <- data.frame(df[433:440, 1:2])
mor <- summarise_each(mor, funs(mean))
rownames(mor) <- c("Mormons")
groups <- dplyr::union(groups, mor)

musli <- data.frame(df[441:656, 1:2])
musli <- summarise_each(musli, funs(mean))
rownames(musli) <- c("Muslims")
groups <- dplyr::union(groups, musli)

ortho <- data.frame(df[657:768, 1:2])
ortho <- summarise_each(ortho, funs(mean))
rownames(ortho) <- c("Orthodox")
groups <- dplyr::union(groups, ortho)

prote <- data.frame(df[769:872, 1:2])
prote <- summarise_each(prote, funs(mean))
rownames(prote) <- c("Protestants")
groups <- dplyr::union(groups, prote)

sci <- data.frame(df[873:880, 1:2])
sci <- summarise_each(sci, funs(mean))
rownames(sci) <- c("Scientologists")
groups <- dplyr::union(groups, sci)

sik <- data.frame(df[881:888, 1:2])
sik <- summarise_each(sik, funs(mean))
rownames(sik) <- c("Sikhs")
groups <- dplyr::union(groups, sik)

spi <- data.frame(df[889:896, 1:2])
spi <- summarise_each(spi, funs(mean))
rownames(spi) <- c("Spiritists")
groups <- dplyr::union(groups, spi)

##Visualising Group Placements

ggplot(groups, aes(x=Dim.1, y=Dim.2)) +
  geom_point(color="#e43085", size=3) +
  labs(x = "Privatisation of Religion", y = "Regulation of Religion") +
  geom_hline(yintercept=0, color="grey") +
  geom_vline(xintercept=0, color="grey") + 
  geom_text_repel(
    label=rownames(groups), size=5
  ) +
  theme_clean() +
  labs(title = "Fig.5 Religious Accommodation of Minorities", subtitle = "Average group placement according to Privatisation of Religion and Regulation of Religion.", caption= "Source: Religion and the State Round 3. Own Elaboration") +
  theme(title= element_text(size = 15), text= element_text(size= 8), axis.text = element_text(size=12), axis.title = element_text(size=14))

##Heatmap

load(file= "Data/Existent Data on Religion/Treated Data/Final Data/Min.rdata")

##Summarising groups
budi <- data.frame(Min[1:64, 3:11])
budi <- summarise_each(budi, funs(mean))
rownames(budi) <- c("Buddhists")

catho <- data.frame(Min[65:104, 3:11])
catho <- summarise_each(catho, funs(mean))
rownames(catho) <- c("Catholics")
groups_original <- dplyr::union(budi, catho)

crist <- data.frame(Min[105:176, 3:11])
crist <- summarise_each(crist, funs(mean))
rownames(crist) <- c("Christians")
groups_original <- dplyr::union(groups_original, crist)

hindus <- data.frame(Min[177:192, 3:11])
hindus <- summarise_each(hindus, funs(mean))
rownames(hindus) <- c("Hindus")
groups_original <- dplyr::union(groups_original, hindus)

jeho <- data.frame(Min[193:216, 3:11])
jeho <- summarise_each(jeho, funs(mean))
rownames(jeho) <- c("Jehovah's Witnesses")
groups_original <- dplyr::union(groups_original, jeho)

je <- data.frame(Min[217:432, 3:11])
je <- summarise_each(je, funs(mean))
rownames(je) <- c("Jews")
groups_original <- dplyr::union(groups_original, je)

morm <- data.frame(Min[433:440, 3:11])
morm <- summarise_each(morm, funs(mean))
rownames(morm) <- c("Mormons")
groups_original <- dplyr::union(groups_original, morm)

muslim <- data.frame(Min[441:656, 3:11])
muslim <- summarise_each(muslim, funs(mean))
rownames(muslim) <- c("Muslims")
groups_original <- dplyr::union(groups_original, muslim)

orth <- data.frame(Min[657:768, 3:11])
orth <- summarise_each(orth, funs(mean))
rownames(orth) <- c("Orthodox")
groups_original <- dplyr::union(groups_original, orth)

prot <- data.frame(Min[769:872, 3:11])
prot <- summarise_each(prot, funs(mean))
rownames(prot) <- c("Protestants")
groups_original <- dplyr::union(groups_original, prot)

scie <- data.frame(Min[873:880, 3:11])
scie <- summarise_each(scie, funs(mean))
rownames(scie) <- c("Scientologists")
groups_original <- dplyr::union(groups_original, scie)

si <- data.frame(Min[881:888, 3:11])
si <- summarise_each(si, funs(mean))
rownames(si) <- c("Sikhs")
groups_original <- dplyr::union(groups_original, si)

spiri <- data.frame(Min[889:896, 3:11])
spiri <- summarise_each(spiri, funs(mean))
rownames(spiri) <- c("Spiritists")
groups_original <- dplyr::union(groups_original, spiri)

groups_original <- round(groups_original, digits=2)

##Adding all values for ranking

groups_original$Sum <- rowSums(groups_original)
groups_original <- arrange(groups_original, desc(Sum))
groups_original = subset(groups_original, select = -c(Sum) )

names(groups_original) <- c("Rites of Passage", "Dietary Laws", "Burial", "Religious Clothing", "Private Holidays", "Clergy Jails", "Clergy Military", "Clergy Hospitals", "Mandatory Religious Education")

groups_original$Group <- rownames(groups_original)

groups_original <- groups_original[,c("Group",names(sort(colSums(groups_original[,c(1:ncol(groups_original)-1)]), decreasing = T)))]

ord <- groups_original$Group
ord <- ord[length(ord):1]
groups_original <- groups_original[-c(10)]

##Heatmap
groups_melt <- melt(groups_original)

groups_melt <- groups_melt %>%
  mutate(Group = fct_relevel(Group, ord))

groups_melt %>%
  ggplot( aes(variable, Group, fill= value))+
  geom_tile(color = "white",
            lwd = 0.5,
            linetype = 1) +
  geom_text(aes(label = value), color = "white", size = 3)+
  xlab("Variable") +
  ylab("Group") +
  ggtitle("Fig.6 Average Minority Group Scores in\nOriginal Dataset")+
  labs(fill = "Restrictions") +
  scale_fill_gradient(low="white",high="#fa3497")+
  theme_clean() +
  theme(legend.position = "none",
        axis.text.x = element_text(angle = 60, vjust = 0, hjust=0, size=10),
        plot.margin=unit(c(0.5,1,0.5,0),"cm"),
        plot.background = element_rect(color=NA), title= element_text(size = 13), text= element_text(size= 6), axis.text.y = element_text(size=10))+
  xlab("")+
  ylab("")+
  scale_x_discrete(position = "top")+
  labs(caption= "Source: Religion and the State Round 3. Own Elaboration")